import requests
import json
import re

url = 'https://kyfw.12306.cn/otn/leftTicket/query?leftTicketDTO.train_date=2020-04-18&leftTicketDTO.from_station=AST&leftTicketDTO.to_station=SYT&purpose_codes=ADULT'
blo_ch = True
# 获取地名对应的代号
def get_name(name,headers):
    url = 'https://www.12306.cn/index/script/core/common/station_name_v10028.js'
    response = requests.get(url,headers = headers)
    res = re.findall(r'@.+?\|(.+?)\|(.+?)\|',response.text)
    for i in res:
        if i[0] == name:
            return i[1]
    return None

# 将信息中的' '值转化为网页类似的那种'--'
def clear_info(list):
    for i in range(len(list)):
        if list[i] == '':
            list[i] = '--'
    return list

# 获取网页信息
def get_html(beg_name,st_name,time):
    url = 'https://kyfw.12306.cn/otn/leftTicket/query'

    params = {
        'leftTicketDTO.train_date': time,
        'leftTicketDTO.from_station': beg_name,
        'leftTicketDTO.to_station': st_name,
        'purpose_codes': 'ADULT'
    }

    response = requests.get(url, headers=headers, params=params)
    return response.text

headers = {
    'Accept': '*/*',
    'Accept-Encoding': 'gzip, deflate, br',
    'Accept-Language': 'zh-CN,zh;q=0.9',
    'Cache-Control': 'no-cache',
    'Connection': 'keep-alive',
    'Cookie': 'JSESSIONID=C2773F4C36F78112286CC15C065CBE44; BIGipServerotn=1624834314.50210.0000; BIGipServerpassport=870842634.50215.0000; RAIL_EXPIRATION=1587438693552; RAIL_DEVICEID=JnZnvc1RxniR2U-FpdJU7typkAqxJ9nCsv3IsxKYyHOsD4dXkU6kuouierC_QhQM9kiuRJWnGQ7YH3wqhxXlCutFDEVxiP1Q5eLbX7OVpImn0_ZIeoxGVQp_eioXoK1muPl6gqQ88ObXLmaW0cFF_-6QBu7XLIa7; route=c5c62a339e7744272a54643b3be5bf64; _jc_save_fromStation=%u978D%u5C71%2CAST; _jc_save_toStation=%u6C88%u9633%2CSYT;',
    'DNT': '1',
    'Host': 'kyfw.12306.cn',
    'If-Modified-Since': '0',
    'Referer': 'https://kyfw.12306.cn/otn/leftTicket/init?linktypeid=dc&fs=%E9%9E%8D%E5%B1%B1,AST&ts=%E6%B2%88%E9%98%B3,SYT&date=2020-04-17&flag=N,N,Y',
    'Sec-Fetch-Dest': 'empty',
    'Sec-Fetch-Mode': 'cors',
    'Sec-Fetch-Site': 'same-origin',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.113 Safari/537.36',
    'X-Requested-With': 'XMLHttpRequest'
}


beg_name = input("请输入出发点:")
st_name = input("请输入到站点:")
time = input("请输入启程时间:")

beg_name = get_name(beg_name,headers)
st_name = get_name(st_name,headers)
if beg_name == None or beg_name == None:
    blo_ch = False
    print("输入地点有错")
if blo_ch:
    # 访问网站并处理json数据
    response = get_html(beg_name, st_name, time)
    response = json.loads(response)
    adder = response['data']['map']
    res = response['data']['result']

    # 将需要的信息提出来
    for item in res:
        info = []
        item = item.split('|')
        info.append(item[3])
        info.append(adder[item[6]])
        info.append(adder[item[7]])
        info.append(item[8])
        info.append(item[9])
        info.append(item[10])
        info.append(item[32])
        info.append(item[31])
        info.append(item[30])
        info.append(item[23])
        info.append(item[28])
        info.append(item[29])
        info.append(item[26])
        last_info = clear_info(info)
        print(last_info)